/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.provider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.scripting.xmltags.MixedSqlNode;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;
import org.apache.ibatis.session.Configuration;

import com.mapperdb.db.ResultEntity;
import com.mapperdb.helper.ResultHelper;
import com.mapperdb.helper.SqlNodeHelper;

/**
 * Mapper SqlSource 提供类
 * @author Administrator
 *
 */
public class BaseMapperProvider implements DbProvider {
	protected MappedStatement mappedStatement;
	protected ResultEntity resultEntity;
	protected Configuration configuration;
	protected SqlNodeHelper sqlNodeHelper;
	protected List<SqlNode> sqlNodes;

	/*------------------------------------------------------------------------*/

	@Override
	public void init(MappedStatement mappedStatement, ResultEntity resultEntity) {
		this.mappedStatement = mappedStatement;
		this.sqlNodes = new ArrayList<SqlNode>();
		this.configuration = mappedStatement.getConfiguration();
		this.sqlNodeHelper = SqlNodeHelper.use(configuration);
		this.resultEntity = resultEntity;
	}

	@Override
	public void destroy() {
		this.sqlNodes = null;
		this.mappedStatement = null;
		this.configuration = null;
		this.sqlNodeHelper = null;
		this.resultEntity = null;
	}

	protected BaseMapperProvider addSqlNode(String text) {
		sqlNodes.add(new StaticTextSqlNode(text));
		return this;
	}

	protected BaseMapperProvider addSqlNode(SqlNode contents) {
		sqlNodes.add(contents);
		return this;
	}

	protected BaseMapperProvider addSqlNode(List<SqlNode> contents) {
		addSqlNode(new MixedSqlNode(contents));
		return this;
	}

	protected SqlNode pkSqlNode() {
		for (Result result : resultEntity.results()) {
			if (result.id()) { return new StaticTextSqlNode(" WHERE " + ResultHelper.formatPropertyEqColumn(result, "id")); }
		}
		throw new RuntimeException("你没有定义主键。");
	}

	protected SqlNode pksSqlNode() {
		List<SqlNode> whereSqlNodes = new ArrayList<SqlNode>();
		for (Result result : resultEntity.results()) {
			if (result.id()) whereSqlNodes.add(new StaticTextSqlNode(" AND " + ResultHelper.formatPropertyEqColumn(result)));
		}
		if (whereSqlNodes.size() == 0) throw new RuntimeException("你没有定义主键。");
		return sqlNodeHelper.newWhereSqlNode(whereSqlNodes);
	}

	protected SqlNode toSqlNode() {
		return new MixedSqlNode(sqlNodes);
	}

	protected String whereSql(Map<String, Object> paramMap) {
		Object sql = paramMap.get("sql");
		return sql == null ? "" : sql.toString();
	}

	protected void addWhereSqlNode(SqlNode contents) {
		addSqlNode(sqlNodeHelper.newWhereSqlNode(contents));
	}

	protected void addWhereSqlNode(String text) {
		addSqlNode(sqlNodeHelper.newWhereSqlNode(text));
	}

	protected void addSetSqlNode(List<SqlNode> contents) {
		addSqlNode(sqlNodeHelper.newSetSqlNode(contents));
	}

	protected void addTrimSqlNode(List<SqlNode> contents, String prefix, String prefixesToOverride, String suffix,
			String suffixesToOverride) {
		addSqlNode(sqlNodeHelper.newTrimSqlNode(new MixedSqlNode(contents), prefix, prefixesToOverride, suffix,
				suffixesToOverride));
	}

	@Override
    public String dynamicSQL(Object record) {
        return "dynamicSQL";
    }
}
